AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )   
include('shared.lua')     

function ENT:Initialize()
	self:SetModel( "models/env/misc/bank_atm/bank_atm.mdl")
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)	
	self:SetSolid(SOLID_VPHYSICS) 
	
	local phys = self:GetPhysicsObject()
		if phys:IsValid() then 
		phys:Wake() 
	end
end     

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end

	local SpawnPos = tr.HitPos + tr.HitNormal * 25

	local ent = ents.Create( "atm" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

local NextTime = 0

function ENT:AcceptInput( name, activator, caller, data )

	if (CurTime() >= NextTime) then
		NextTime = CurTime() +  1

    if ( name == "Use" && ValidEntity( caller ) && caller:IsPlayer( ) ) then
		if file.Exists("SS/ATM/User_"..caller:UniqueID().."/HasOpenedBefore.txt") then
			if caller.OpenedATM == true then
				umsg.Start("ATMMain", caller)
				umsg.End()			
			else
				umsg.Start("ATMFirstEntry", caller)
				umsg.End()
			end
		else
		
			umsg.Start("ATMPin", caller)
			umsg.End()
			end
		end
	end
end

concommand.Add("SavePIN", function( ply, cmd, args )

local trace = ply:GetEyeTrace()
	
	if trace.Entity and string.lower(trace.Entity:GetClass()) == "atm" then
		if args[1] then
			ply:SetPData("PIN", args[1])
			ply:SendLua("GAMEMODE:AddNotify(\"Your PIN has now been set!\", NOTIFY_GENERIC, 5)")
			file.Write("SS/ATM/User_"..ply:UniqueID().."/HasOpenedBefore.txt", "I love you guys <3")
			umsg.Start("ATMMain", ply)
			umsg.End()
		end
	end
end)

concommand.Add("VerifyPIN", function( ply, cmd, args )

local trace = ply:GetEyeTrace()
	
	if trace.Entity and string.lower(trace.Entity:GetClass()) == "atm" then
		if args[1] then
			if ply:GetPData("PIN") == args[1] then
				ply:SendLua("GAMEMODE:AddNotify(\"Welcome to your account!\", NOTIFY_GENERIC, 5)")
				ply.OpenedATM = true
				umsg.Start("ATMMain", ply)
				umsg.End()
			else
				ply:SendLua("GAMEMODE:AddNotify(\"Your PIN did not match!\", NOTIFY_ERROR, 5)")
			end
		end
	end
end)

concommand.Add( "_Deposit_", function( ply, cmd, args )

	local trace = ply:GetEyeTrace()
	
	if string.find(args[1], "-") then ply:SendLua("GAMEMODE:AddNotify(\"Error! You cannot use minus numbers.\", NOTIFY_ERROR, 5)") return end

	if trace.Entity and string.lower(trace.Entity:GetClass()) == "atm" then
		if tonumber(args[1]) then
			
			local Thingy = math.Round(args[1])
			
			if ply:CanAfford(Thingy) then
				ply:TakeMoney(Thingy)
				ply:GiveATMCash(Thingy)
			else
				ply:SendLua("GAMEMODE:AddNotify(\"You cannot afford $"..args[1].."!\", NOTIFY_ERROR, 5)")
			end
		end
	end
end )

concommand.Add( "_Withdraw_", function( ply, cmd, args )

	local trace = ply:GetEyeTrace()
	
	if string.find(args[1], "-") then ply:SendLua("GAMEMODE:AddNotify(\"Error! You cannot use minus numbers.\", NOTIFY_ERROR, 5)") return end
	
	if trace.Entity and string.lower(trace.Entity:GetClass()) == "atm" then
		if tonumber(args[1]) then
		
			local Thingy = math.Round(args[1])
			
			if ply:HasATMAmount(Thingy) then
				ply:TakeATMCash(Thingy)
				ply:GiveMoney(Thingy)
			else
				ply:SendLua("GAMEMODE:AddNotify(\"You do not have $"..args[1].." within your account!\", NOTIFY_ERROR, 5)")
			end
		end
	end
end )

function DisconnectATM(ply)
	ply.OpenedATM = false
end

function SpawnATM(ply)
	ply.OpenedATM = false
end

hook.Add("PlayerDisconnected", "DisconnectATM", DisconnectATM)
hook.Add("PlayerInitialSpawn", "SpawnATM", SpawnATM)
